package com.dragee.modules.work.controller;

import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.modules.sys.entity.LoginUser;
import com.dragee.modules.sys.po.RolePo;
import com.dragee.modules.work.entity.TeamCoachEntity;
import com.dragee.modules.work.entity.TeamEntity;
import com.dragee.modules.work.service.TeamCoachService;
import com.dragee.modules.work.service.TeamService;
import com.dragee.utils.LoginUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 教练员
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-04-19 09:42:02
 */
@RestController
@RequestMapping("work/teamcoach")
@Api(tags = "work - 教练员")
public class TeamCoachController {
    @Autowired
    private TeamCoachService teamCoachService;
    @Autowired
    private TeamService teamService;

    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("教练员列表")
    public R list(@RequestParam Map<String, Object> params){
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        RolePo role = loginUser.getRole();
        if (role.getRoleKey().equals("duiwu")) {
            // 队伍登录
            TeamEntity team = teamService.getTeamByUserId(loginUser.getUserId().toString());
            params.put("teamKey", team.getId());
        }
        PageUtils page = teamCoachService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("教练员信息")
    public R info(@PathVariable("id") Long id){
		TeamCoachEntity teamCoach = teamCoachService.getById(id);
        return R.ok().put("data", teamCoach);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改教练员")
    public R saveOrUpdate(@RequestBody TeamCoachEntity teamCoach){
        TeamEntity team = teamService.getTeamByUserId(LoginUserUtil.getLoginUserId().toString());
        teamCoach.setTeamKey(team.getId().toString());
        teamCoach.setTeamId(team.getCode());
        teamCoachService.saveOrUpdate(teamCoach);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("教练员删除")
    public R delete( String id){
		teamCoachService.removeById(Long.parseLong(id));
        return R.ok();
    }

    @GetMapping("delete-by-ids")
    @ApiOperation("教练员批量删除")
    public R deletes(String ids){
        String[] idArr = ids.split(",");
        Arrays.stream(idArr).forEach(id -> teamCoachService.removeById(Long.parseLong(id)));
        return R.ok();
    }

}
